package com.wzg.mytableview.Custom;


import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.bin.david.form.utils.DensityUtils;
import com.wzg.mytableview.R;


/**
 * author：wzg
 * date:
 * desc:通过tableLayout实现table效果
 */
public class TableLayoutActivity extends AppCompatActivity {


    private TableLayout tableLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_layout);

        tableLayout = findViewById(R.id.tableLayout);


        int dp20 = DensityUtils.dp2px(this, 20);
        int dp15 = DensityUtils.dp2px(this, 15);
        int dp80 = DensityUtils.dp2px(this, 80);
        int dp200 = DensityUtils.dp2px(this, 200);


        for (int i = 0; i < 10; i++) {

            //5个字段
            TableRow tr = new TableRow(this);
            //设置分割线
            tr.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.diver_line));
            tr.setShowDividers(LinearLayout.SHOW_DIVIDER_BEGINNING | LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_END);

            for (int j = 0; j < 5; j++) {

                TextView tv = new TextView(this);
                tv.setPadding(dp20, dp15, dp20, dp15);
                tv.setMinWidth(dp80);
                tv.setGravity(Gravity.CENTER);
                tv.setMaxWidth(dp200);
                tv.setMaxLines(4);

                if (i == 1 && j == 1) {
                    tv.setText("tv11111111111111111111111111111111");
                    tv.setOnClickListener(v -> {

                    });

                } else tv.setText("tv" + i);

                tr.addView(tv);
            }

            tableLayout.addView(tr, new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        }


    }


    public interface OnClickCall {

        void call(View v, int row, int column);
    }


}